package org.jks.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.exception.ConstraintViolationException;
import org.jks.config.Config;
import org.jks.model.Department;
import org.jks.request.model.Ids;
import org.jks.response.model.ActionResult;
import org.jks.response.model.UITable;
import org.jks.service.DepartmentService;
import org.jks.util.JsonUtil;
import org.jks.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/department")
public class DepartmentController {
	@Autowired
	DepartmentService departmentService;

	@RequestMapping("/add")
	public void add(Department department, HttpServletRequest request,
			HttpServletResponse response) {
		ActionResult ar = new ActionResult();
		if(!StringUtils.isEmpty(department.getName())){
			try {
				departmentService.save(department);
				ar.setSuccess(true);
				ar.setData(department);
			}
			catch( ConstraintViolationException e){
				ar.setMsg("部门名称冲突");
			}
			catch (Exception e){
				ar.setMsg("添加部门失败");
			}
		}
		else{
			ar.setMsg("部门名不能为空");
		}
		
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete")
	public void delete(Ids ids, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			departmentService.delete(ids.getIds());
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg("删除部门失败");
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/delete/{id}")
	public void delete(@PathVariable long id, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			departmentService.delete(id);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/detail/{id}")
	public ModelAndView detail(@PathVariable long id, HttpServletRequest request, HttpSession session, HttpServletResponse response, Map<String,Object> model){
		try {
			Department department = departmentService.findById(id);
			model.put("department", JsonUtil.Object2Json(department));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Config.init(request, response, session, model);
		return new ModelAndView("sysMgr/dpt_detail", model);
	}
	
	@RequestMapping("/update")
	public void update(Department department, HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			departmentService.update(department);
			ar.setSuccess(true);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
	
	@RequestMapping("/all")
	public void all(HttpServletRequest request, HttpServletResponse response){
		ActionResult ar = new ActionResult();
		try{
			List<Department> dpts = departmentService.all();
			ar.setSuccess(true);
			UITable table = new UITable();
			table.setRows(dpts);
			ar.setData(table);
		}
		catch(Exception e){
			ar.setMsg(e.getMessage());
			e.printStackTrace();
		}
		ResponseUtil.returnJson(response, ar);
	}
}
